#ifndef __WEB_SOCKET_CLIENT_H__
#define __WEB_SOCKET_CLIENT_H__
#include "cocos2d.h"
#include <list>
#include <thread>
#include "json/rapidjson.h"
#include "json/document.h"
#include "json/stringbuffer.h"
#include "json/writer.h"
#include "network/SocketIO.h"
#include "manager/GlobalData.h"
#include "manager/MessageHandler.h"
#include "utility/log/LogUtil.h"
#include "utility/string/FormatUtil.h"
#include "VersionDefine.h"

using namespace cocos2d;
using namespace cocos2d::network;

class WebSocketClient: public SocketIO::SIODelegate
{
public:
	static WebSocketClient* getInstance();
	bool isOpen();
	void sendTest();

	void send(const char* mess);
	void send(const char* apiName,ValueMap mess);
	void sendImmediately(const char* mess);
	void sendImmediately(const char* apiName,ValueMap mess);
	int getConnectState();
private:
	WebSocketClient();
	~WebSocketClient();
	
	void init();
	void conect();
	virtual void onConnect(SIOClient* client);
	virtual void onMessage(cocos2d::network::SIOClient* client, const std::string& data);
	virtual void onClose(cocos2d::network::SIOClient* client);
	virtual void onError(cocos2d::network::SIOClient* client, const std::string& data);

	void sendTh();

	void handle(const char* mess);

	std::string makeApiJson(const char* apiName, ValueMap mess);
private:
	SIOClient* m_pSocketIOClient;
	int m_nConnectState;
	std::list<String*> m_messageQueue;
	std::list<String*> m_messageApiQueue;
	std::mutex   m_messageQueueMutex;
	std::mutex   m_revMessageQueueMutex;

	MessageHandler* m_messageHandler;
};
#endif